package com.valentinalee.bms.web.servlet.filemanage;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.valentinalee.bms.entity.FileSystemVO;
import com.valentinalee.bms.entity.ManagerVO;
import com.valentinalee.bms.service.FileSystemService;
import com.valentinalee.bms.service.impl.FileSystemServiceImpl;

public class FileUploadServlet extends HttpServlet {
	FileSystemService iFileService = new FileSystemServiceImpl();
	/**
	 * Constructor of the object.
	 */
	public FileUploadServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//文件上传的内容
				response.setContentType("text/html;charset=utf-8");//设置一些基本属性  防止乱码
				response.setCharacterEncoding("UTF-8");
				DiskFileItemFactory itemFactory  = new DiskFileItemFactory();//创建一个文件项对象
				ServletFileUpload fileUpload = new ServletFileUpload(itemFactory);
				boolean isSuccessed =false;//判断文件上传是否成功
				String uploadFileName = "";//定义一个局部变量用于存放上传后的文件名称
				Date d = null;
				try {
					List<FileItem> list = fileUpload.parseRequest(request);
					for(FileItem li :list){
						String name = li.getName();//获取名称
						String filedName = li.getFieldName();//获取取值
						boolean formFiled = li.isFormField();
						if(!formFiled){
							String uuid = UUID.randomUUID().toString();
							String sName = name.substring(name.indexOf("."));
							if(filedName!=null&&!"".equals(filedName)){
								String path ="E:\\二阶段项目\\bms\\WebRoot\\WEB-INF\\upload";//文件上传的绝对路径Root....
								uploadFileName =uuid+sName;
								File file =new File(path,uploadFileName);
								li.write(file);
							}
						}
					}
					isSuccessed = true;
					d = new Date();
					response.getWriter().write("文件上传成功!");
				} catch (FileUploadException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//添加记录 FileSystem 所有属性
				FileSystemVO fs = new FileSystemVO();
				//1.文件路径 
				String path ="E:\\二阶段项目\\bms\\WebRoot\\WEB-INF\\upload";
				fs.setFilePath(path);
				//2.文件名称
				String name = uploadFileName;
				fs.setFileName(name);
				//3.上传人
				HttpSession session = request.getSession(true);
				ManagerVO vo  = (ManagerVO)session.getAttribute("currentManager");
				
				fs.setFileUser(vo.getName());
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//4.上传时间
				String uploadTime =  sdf.format(new Date());
				fs.setHandleTime(uploadTime);
				//5.处理类型
				String handleflag="u";
				fs.setHandleFlag(handleflag);
				int rflag =0;
				FileSystemVO rvo =  new FileSystemVO();
				//调用业务层service
				if(isSuccessed){
					try {
						rvo =  iFileService.addFileRmark(fs);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					response.getWriter().write("文件记录成功");
				}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
